<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function action_xuat_xu($action_info, $option)
{
	$action['submit']='submit_xuat_xu';
	$action['edit']='member/vattu/edit_source/';
	action_display($action,$action_info, $option);
}

function action_taisan($action_info, $option)
{
	$action['submit']='submit_taisan';
	$action['edit']='member/taisan/edit_taisan/';
	action_display($action,$action_info, $option);
}

function action_chung_tu_ts($action_info, $option)
{
	$action['submit']='submit_chung_tu_ts';
	$action['edit']='member/taisan/edit_chung_tu/';
	action_display($action,$action_info, $option);
}

function action_hang_muc($action_info, $option)
{
	$action['submit']='submit_hang_muc';
	$action['edit']='member/taichinh/edit_hang_muc/';
	action_display($action,$action_info, $option);
}

function action_danh_muc($action_info, $option)
{
	$action['submit']='submit_category';
	$action['edit']='member/vattu/edit_category/';
	action_display($action,$action_info, $option);
}

function action_kho($action_info, $option)
{
	$action['submit']='submit_kho';
	$action['edit']='member/vattu/edit_store/';
	action_display($action,$action_info,$option);
}

function action_bill($action_info)
{
	if($action_info['type'] == 1)
	{
		$action['submit']='submit_bill_import';
		$action['edit']='member/vattu/edit_bill_import/';
		action_display($action,$action_info);
	}
	else {
		$action['submit']='submit_bill_export';
		$action['edit']='member/vattu/edit_bill_export/';
		action_display($action,$action_info,2);
	}
	
}

function action_display($action,$action_info,$style=1)
{
	if($style == 2)
	{
		echo '<span id="lblactionitem'. $action_info["id"] .'">';
		$act = $action['submit'].'('.$action_info["id"].')';
		if($action_info['status'] == 0) // Vua moi them moi
		{
			echo anchor($action['edit'].$action_info['id'], '<img src="' . base_url() . 'application/views/templates/default/images/edit.png" title="' . lang('edit') . '" border="0">');
		}
		elseif($action_info['status'] == 1) //da submit
		{
			echo '<img src="' . base_url() . 'application/views/templates/default/images/disable.png" title="' . lang('disable') . '" border="0">';
		}
		else // chua sua dung
		{
	
		}
		echo '</span>';
	}
	else {
		echo '<span id="lblactionitem'. $action_info["id"] .'">';
		$act = $action['submit'].'('.$action_info["id"].')';
		if($action_info['status'] == 0) // Vua moi them moi
		{
			echo  '<img src="' . base_url() . 'application/views/templates/default/images/enable.png" title="" onclick="'.$act.'" />';
			echo anchor($action['edit'].$action_info['id'], '<img src="' . base_url() . 'application/views/templates/default/images/edit.png" title="' . lang('edit') . '" border="0">');
		}
		elseif($action_info['status'] == 1) //da submit
		{
			echo '<img src="' . base_url() . 'application/views/templates/default/images/disable.png" title="' . lang('disable') . '" border="0">';
		}
		else // chua sua dung
		{
		
		}
		echo '</span>';
	}
}

function get_typealias($type)
{
	$_arr_type = array(1=>"Vật tư dùng 1 lần",2=>"Vật tư dùng nhiều lần",3=>'Công cụ, dụng cụ, ttb',4=>'Phế liệu loại 1',5=>'Phế liệu ko dùng nữa');
	return $_arr_type[$type];
}

function get_typets($type)
{
	$_arr_type = array(1=>"Mới",2=>"Đã dùng",3=>'Đã sửa',4=>'Chờ thanh lý',5=>'Thanh lý');
	return $_arr_type[$type];
}

function status_item($status,$item_id)
{
	echo '<span id="lblstatusitem'. $item_id .'">';
	if($status == 0) // Vua moi them moi
	{
		echo '<input type="checkbox" id="itemid'.$item_id .'" name="itemcheck[]" value="'.$item_id.'" class="case" >';
	}
	elseif($status == 1) //da submit
	{
		echo 'Đã xác nhận';
	}
	else // chua sua dung
	{
		
	}
	echo '</span>';
}

function action_item($action_info, $option)
{
	$action['submit']='submit_item';
	$action['edit']='member/vattu/edit_vattu/';
	action_display($action,$action_info, $option);
}

function tool_bar_item($action_infos, $colspan)
{
	$html = '<tr>';
	$html = $html . '<th colspan="'.$colspan.'" align="right">';
	$html = $html . '<a onclick="submit();" onmouseover="this.style.cursor=\'pointer\';">Xác nhận</a>';
	$html = $html . '</th>';
	$html = $html . '<th align="right" class="ta-center">';
	$html = $html . '<input type="checkbox" name="selectall" id="selectall" />';
	$html = $html . '</th>';
	$html = $html . '</tr>';
	$count = 0;
	foreach($action_infos as $action_info)
	{
		if($action_info['status']== 0)
		{
			$count = 1; break;
		}
	}
	if($count == 1)
	{
		echo $html;
	}
}
/*
 * display drop list box to select class
 * $prams is array with key is properties of select tag
 */
function droplistbox_class($object,$prams=null)
{
	$html = '';
	$header = '<select id="" name="" class="">';	
	$footer = '</select>';
	$arr = array();
	if(is_array($object))
	{
		foreach($object as $item)
		{
			$arr[$item['id']] = $item['tenlop'];
		} 
	}
	elseif(is_object($object)) 
	{
		foreach($object as $item)
		{
			$arr[$item->id] = $item->tenlop;
		}
	}	
	else{
		echo $html='';
		return;
	}	
	$selected = 'NA';
	$expanded = '';
	if(is_array($prams))
	{
		$selected = $prams['selected'];	
		
		foreach($prams as $key=>$value)
		{
			$expanded = $expanded . ' '. $key.'='.'"'.$value.'"';
		} 
	}	
	echo form_dropdown($prams['name'],$arr,$selected,$expanded);
}

function droplistbox_store($object,$prams=null)
{
	$html = '';
	$header = '<select id="" name="" class="">';
	$footer = '</select>';
	$arr = array();
	if(is_array($object))
	{
		foreach($object as $item)
		{
			$arr[$item['id']] = $item['name'];
		}
	}
	elseif(is_object($object))
	{
		foreach($object as $item)
		{
			$arr[$item->id] = $item->name;
		}
	}
	else{
		echo $html='';
		return;
	}
	$selected = 'NA';
	$expanded = '';
	if(is_array($prams))
	{
		$selected = $prams['selected'];

		foreach($prams as $key=>$value)
		{
			$expanded = $expanded . ' '. $key.'='.'"'.$value.'"';
		}
	}
	echo form_dropdown($prams['name'],$arr,$selected,$expanded);
}

function convert_number_to_words($number) {
	$hyphen      = ' ';
	$conjunction = '  ';
	$separator   = ' ';
	$negative    = 'negative ';
	$decimal     = ' point ';
	$dictionary  = array(
			0                   => 'Không',
			1                   => 'Một',
			2                   => 'Hai',
			3                   => 'Ba',
			4                   => 'Bốn',
			5                   => 'Năm',
			6                   => 'Sáu',
			7                   => 'Bảy',
			8                   => 'Tám',
			9                   => 'Chín',
			10                  => 'Mười',
			11                  => 'Mười một',
			12                  => 'Mười hai',
			13                  => 'Mười ba',
			14                  => 'Mười bốn',
			15                  => 'Mười năm',
			16                  => 'Mười sáu',
			17                  => 'Mười bảy',
			18                  => 'Mười tám',
			19                  => 'Mười chín',
			20                  => 'Hai mươi',
			30                  => 'Ba mươi',
			40                  => 'Bốn mươi',
			50                  => 'Năm mươi',
			60                  => 'Sáu mươi',
			70                  => 'Bảy mươi',
			80                  => 'Tám mươi',
			90                  => 'Chín mươi',
			100                 => 'trăm',
			1000                => 'nghìn',
			1000000             => 'triệu',
			1000000000          => 'tỷ',
			1000000000000       => 'nghìn tỷ',
			1000000000000000    => 'ngàn triệu triệu',
			1000000000000000000 => 'tỷ tỷ'
	);
	if (!is_numeric($number)) {
		return false;
	}
	if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
		// overflow
		trigger_error(
		'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
		E_USER_WARNING
		);
		return false;
	}
	if ($number < 0) {
		return $negative . convert_number_to_words(abs($number));
	}
	$string = $fraction = null;
	if (strpos($number, '.') !== false) {
		list($number, $fraction) = explode('.', $number);
	}
	switch (true) {
		case $number < 21:
			$string = $dictionary[$number];
			break;
		case $number < 100:
			$tens   = ((int) ($number / 10)) * 10;
			$units  = $number % 10;
			$string = $dictionary[$tens];
			if ($units) {
				$string .= $hyphen . $dictionary[$units];
			}
			break;
		case $number < 1000:
			$hundreds  = $number / 100;
			$remainder = $number % 100;
			$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
			if ($remainder) {
				$string .= $conjunction . convert_number_to_words($remainder);
			}
			break;
		default:
			$baseUnit = pow(1000, floor(log($number, 1000)));
			$numBaseUnits = (int) ($number / $baseUnit);
			$remainder = $number % $baseUnit;
			$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
			if ($remainder) {
				$string .= $remainder < 100 ? $conjunction : $separator;
				$string .= convert_number_to_words($remainder);
			}
			break;
	}
	if (null !== $fraction && is_numeric($fraction)) {
		$string .= $decimal;
		$words = array();
		foreach (str_split((string) $fraction) as $number) {
			$words[] = $dictionary[$number];
		}
		$string .= implode(' ', $words);
	}
	return $string;
}